package service

import (
	"context"
	"encoding/json"
	"errors"
	"github.com/benji/software/Entities"
	"github.com/benji/software/dao"
	"github.com/benji/software/model"
	log "github.com/sirupsen/logrus"
	"math/rand"
	"strconv"
	"strings"
)

func GetQuestions(level int64,size int )([]*Entities.Questions,error){
	questions,err := dao.GetQuestionsByLevel(level)
	if err != nil {
		log.Errorf("Service fail to get questions")
		return nil,err
	}
	res := make([]*Entities.Questions,0)
	for i:=size;i>0;i--{
		shuffle(questions)
		temp := Entities.Questions{}
		temp.Question = questions[0].Question
		temp.ID = questions[0].ID
		res = append(res,&temp)
	}
	return res,nil
}
//GetAnswer: 首先拿到正确答案,然后和学生的做对比,将学生的draft记下来
func GetAnswers(ctx context.Context,studentAnswers Entities.StudentAnswers)(*Entities.RightAnswers,error){
		userID,ok := ctx.Value("id").(int)
		if ! ok {
			log.Errorf("Fail to get userID")
			return nil,errors.New("fail to get userID")
		}
		keys := make([]int,0)
		for key,_:= range studentAnswers.Questions{
			keys = append(keys,key)
		}
		log.Infof("keys: %v",keys)
		questions,err := dao.GetQuestionsByID(keys)
		if err != nil {
			log.Errorf("Service fail to GetQuestionsByID")
			return nil,err
		}
		res := Entities.RightAnswers{}
		res.Answers = make(map[int]Entities.Answer,0)
		history := model.History{}
		rightAnswerList := make([]int,0)
		wrongAnswerList := make([]int,0)
		commitAnswer := make(map[int]Entities.CommitAnswer,0)
		for _, question := range questions{
			studentAnswer := studentAnswers.Questions[question.ID]
			rightAnswer := Entities.Answer{
				Question: question.Question,
				Answer: question.Answer,
			}
			res.Answers[question.ID] = rightAnswer
			// 去除空格
			studentAnswer.Answer = strings.TrimSpace(studentAnswer.Answer)
			if studentAnswer.Answer == question.Answer {
				rightAnswerList = append(rightAnswerList,question.ID)
			}else{
				wrongAnswerList = append(wrongAnswerList,question.ID)
			}
			tempAnswer := Entities.CommitAnswer{}
			tempAnswer.Question = question.Question
			tempAnswer.RAnswer = question.Answer
			tempAnswer.SAnswer = studentAnswer.Answer
			commitAnswer[question.ID] = tempAnswer
		}
		studentCommitData,err := json.Marshal(commitAnswer)
		if err != nil {
			log.Errorf("Fail to Marshal studentAnswers.Questions, err:%v",err)
			return nil ,err
		}
		history.StudentCommit = string(studentCommitData)
		history.Student = userID
		history.RightAnswer = intArray2string(rightAnswerList)
		history.WrongAnswer = intArray2string(wrongAnswerList)
		log.Infof("rightAnswerList: %v",len(rightAnswerList))
		log.Infof("wrongAnswerList: %v",len(wrongAnswerList))
		if len(rightAnswerList) == 0{
			history.Grades = 0
		}else{
			history.Grades = len(rightAnswerList)/(len(rightAnswerList)+len(wrongAnswerList))*100
		}
		if err := dao.AddHistory(&history);err !=nil {
			log.Infof("fail to add history,err:%v",err)
			return nil,err
		}
		return &res,nil
}
func intArray2string(s []int)string{
	stringList := make([]string,0)
	for _,val := range s {
		stringList = append(stringList,strconv.FormatInt(int64(val),10))
	}
	return strings.Join(stringList,",")
}
func shuffle(s []*model.Question){
	for i := len(s);i>0;i--{
		lastIndex := i-1
		idx := rand.Intn(i)
		s[idx],s[lastIndex] = s[lastIndex],s[idx]
	}
}